package api

import (
	"strconv"

	"github.com/Blog/global"
	"github.com/Blog/internal/service"
	"github.com/Blog/pkg/app"
	pkg "github.com/Blog/pkg/errcode"
	"github.com/gin-gonic/gin"
)

type Tag struct{}

func NewTag() Tag {
	return Tag{}
}

//@Summary 获取多个标签
//@Produce json
//@Param name query string false "标签名称" maxlength(100)
//@Router /api/v1/tags [get]
func (t Tag) List(c *gin.Context) {
	Param := struct {
		Name  string `form:"name" binding:"max=100"`
		State uint8  `form:"state,default=1" binding:"oneof=0 1"`
	}{}

	response := app.NewResponse(c)
	valid, errs := app.BindAndValid(c, &Param)
	if !valid {
		global.Logger.Infof("app.BindAndValid errs:%v", errs)
		errRsp := pkg.InvalidParms.WithDetails(errs.Errors()...)
		response.ToErrorResponse(errRsp)
		return
	}

	response.ToResponse(gin.H{})
	return
}
func (t Tag) Create(c *gin.Context) {}
func (t Tag) Update(c *gin.Context) {}
func (t Tag) Delete(c *gin.Context) {}
func (t Tag) Get(c *gin.Context) {
	response := app.NewResponse(c)
	idr := c.Param("id")
	id, err := strconv.Atoi(idr)
	if err != nil {
		errRsp := pkg.InvalidParms.WithDetails(err.Error())

		response.ToErrorResponse(errRsp)
		return
	}
	reqParam := service.GetTagByIDRequest{ID: uint32(id)}
	svc := service.New(c.Request.Context())
	tag, err := svc.GetTag(&reqParam)
	if err != nil {
		global.Logger.Errorf("svc.GetTag.fail.err=%+v\n", err)
		return
	}
	response.ToResponse(tag)
}
